package com.kaci.springbootormlite;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;

public class ServiceRegistrar {
    private static final String MULTICAST_ADDRESS = "225.0.0.1";
    private static final int PORT = 9876;
    private static final String SERVICE_NAME = "MyService";

    public static void main(String[] args) {
        try {
            MulticastSocket socket = new MulticastSocket();
            InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
            socket.setNetworkInterface(NetworkInterface.getByInetAddress(InetAddress.getLocalHost()));

            // Broadcast service d every 5 seconds
            while (true) {
                String message = SERVICE_NAME + " is available at " + InetAddress.getLocalHost().getHostAddress();
                byte[] buffer = message.getBytes();
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, PORT);
                socket.send(packet);
                System.out.println("Service registered: " + message);
                Thread.sleep(5000); // Wait for 5 seconds before sending the next registration
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}